package com.example.emailservice.Email;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Repository;

import javax.mail.internet.MimeMessage;
import java.io.File;

@Repository
public class SendEmailService {

    @Autowired
    private JavaMailSender mailSender; //自动注入的Bean

    @Value("${spring.mail.username}")
    private String Sender; //读取配置文件中的参数 -- 发件人

    /**
     * 发送简单文本邮件
     * @param EmailModel
     */
    public void sendSimpleMail(EmailInfo EmailModel){
        try {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(Sender);
            message.setTo(EmailModel.getTo());
            message.setSubject(EmailModel.getSubject());
            message.setText(EmailModel.getContent());
            mailSender.send(message);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 发送Html邮件
     * @param EmailModel
     */
    public void sendHtmlMail(EmailInfo EmailModel){
        MimeMessage message = null;
        try {
            message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(Sender);
            helper.setTo(EmailModel.getTo());
            helper.setSubject(EmailModel.getSubject());
            //StringBuffer sb = new StringBuffer();
            //sb.append("<h1>大标题-h1</h1>").append("<p style='color:#F00'>红色字</p>").append("<p style='text-align:right'>右对齐</p>");
            //helper.setText(sb.toString(), true);
            helper.setText(EmailModel.getContent(), true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        mailSender.send(message);
    }

    /**
     * 发送带附件的邮件
     * @param EmailModel
     */
    public void sendAttachmentsMail(EmailInfo EmailModel){
        MimeMessage message = null;
        try {
            message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(Sender);
            helper.setTo(EmailModel.getTo());
            helper.setSubject(EmailModel.getSubject());
            helper.setText(EmailModel.getContent());  //发送普通邮件，发送html邮件要加一个boolean参数
            //注意项目路径问题，自动补用项目路径
            if(EmailModel.getAttachPath() != null){
                for (String path : EmailModel.getAttachPath()){
                    //FileSystemResource file = new FileSystemResource(new File("src/main/resources/static/image/picture.jpg"));
                    FileSystemResource file = new FileSystemResource(new File(path));
                    //加入邮件
                    helper.addAttachment(file.getFilename(), file);
                }
            }
        } catch (Exception e){
            e.printStackTrace();
        }
        mailSender.send(message);
    }
}
